﻿#include <iostream>
using namespace std;

class A {
public:
    A(int size) : size_(size) {
        data_ = new int[size];
    }

    A() : size_(0), data_(nullptr) {}

    // 深拷贝：分配新内存并复制数据
    A(const A& a) {
        size_ = a.size_;
        data_ = new int[size_];
        for (int i = 0; i < size_; ++i) {
            data_[i] = a.data_[i];
        }
        cout << "deep copy" << endl;
    }

    ~A() {
        delete[] data_;
    }

    int* data_;
    int size_;
};

int main() {
    A a(10);
    for (int i = 0; i < 10; ++i)
        a.data_[i] = i;

    A b = a;  // 深拷贝
    cout << "b " << b.data_ << endl;
    cout << "a " << a.data_ << endl;

    // 验证数据独立性
    b.data_[0] = 999;
    cout << "a[0]: " << a.data_[0] << endl;  // 应输出0
    cout << "b[0]: " << b.data_[0] << endl;  // 应输出999

    return 0;
}

